﻿using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using RPAVIEW.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.EditPage.flows.KeyBoardInputN {
    /// <summary>
    /// SpecialKeyBoard.xaml 的交互逻辑
    /// </summary>
    public partial class SpecialKeyBoard:Window {
        private TextBox inputBox;
        public SpecialKeyBoard(TextBox inputBox) {
            InitializeComponent();
            this.InitKeyBoard();
            this.inputBox = inputBox;
        }
        public void InitKeyBoard() {
            Style style = (Style)this.FindResource("MaterialDesignPaperSecondaryLightButton");
            Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RPAVIEW.resource.SoftKeyBoardKeyDef.json");
             using(BufferedStream bs = new BufferedStream(stream)) { 
                byte[] bytes = new byte[bs.Length];
                bs.Read(bytes, 0, bytes.Length);
                string JsonTree = Encoding.UTF8.GetString(bytes);
                List<SoftKey>? tree = JsonConvert.DeserializeObject<List<SoftKey>>(JsonTree);
                foreach(SoftKey key in tree) { 
                    Button b = new Button();
                    b.Padding = new Thickness(10, 3, 10,3);
                    b.Style = style;
                    if(key.Name.StartsWith("PackIconKind")) {
                        PackIcon icon = new PackIcon();
                        icon.Kind = (PackIconKind)Enum.Parse(typeof(PackIconKind), key.Name.Substring(key.Name.IndexOf(".")+1));
                        b.Content = icon;
                    } else { 
                        b.Content = key.Name;
                    }
                    b.DataContext = key;
                    b.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.ButtonClick), true);
                    this.keyboard.Children.Add(b);
                }
            }
        }

        private void ButtonClick(object sender, MouseButtonEventArgs e) { 
            Button clickButton = (Button)sender;
            SoftKey key = clickButton.DataContext as SoftKey;
            this.inputBox.Text += key.Text;
        }
    }
}
